package com.leesche.zcsp.node.pay.wecha.data;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.leesche.zcsp.node.utils.PayConfig;
import com.leesche.zcsp.node.utils.RequestHandler;

public class RequestParam {
	private static final Logger LOGGER = Logger.getLogger(RequestParam.class);

	private TreeMap<String, String> map = new TreeMap<String, String>();

	private RequestHandler reqHandler = null;

	public void putParam(String key, String val) {
		map.put(key, val);
	}

	public RequestParam(RequestHandler req) {
		reqHandler = req;
	}

	public String toString() {
		// map.put("signType", "MD5");

		String sign = reqHandler.createSign(map);
		putParam("sign", sign);

		StringBuffer sb = new StringBuffer();
		Set es = map.entrySet();
		Iterator it = es.iterator();
		boolean isFirst = true;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			{
				if (isFirst == true) {
					isFirst = false;
					sb.append(k + "=" + v);
				} else {
					sb.append("&" + k + "=" + v);
				}
			}
		}

		String data = sb.toString();
		LOGGER.info("request param:" + data);
		return data;
	}
}
